#!/bin/bash
set -eu

touch $LOGDIR/skip.updatebase

mount --rbind /dev $FAI_ROOT/dev
mount --make-private $FAI_ROOT/dev
mount --bind /proc $FAI_ROOT/proc
mount --make-private $FAI_ROOT/proc
mount --bind /sys $FAI_ROOT/sys
mount --make-private $FAI_ROOT/sys
mount -t tmpfs none $FAI_ROOT/var/tmp
mount --make-private $FAI_ROOT/var/tmp
mount -t tmpfs none $FAI_ROOT/tmp
mount --make-private $FAI_ROOT/tmp

# When systemd-resolved is installed in the FAI root, it will overwrite
# /etc/resolv.conf with a symlink to /run/systemd/resolve/stub-resolv.conf
# regardless of whether the target exists or not.  If it doesn't, then we're
# left with broken DNS inside the FAI root for the rest of the installation
# process.  Avoid that condition by pre-populating stub-resolv.conf.  Note that
# the file may already exist if the host is running systemd-resolved
if [ ! -e "$FAI_ROOT/run/systemd/resolve/stub-resolv.conf" ]; then
    mkdir -p "$FAI_ROOT/run/systemd/resolve"
    cp -v /etc/resolv.conf "$FAI_ROOT/run/systemd/resolve/stub-resolv.conf"
fi

aptopt="-y -o Dpkg::Options::=--force-confnew"
$ROOTCMD apt-get -y update
$ROOTCMD apt-get $aptopt dist-upgrade < /dev/null
